package com.inforich.disposal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.inforich.report.domain.ResourceRubbishProduce;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.disposal.mapper.DisposalLandTerminalVehicleApproachMapper;
import com.inforich.disposal.domain.DisposalLandTerminalVehicleApproach;
import com.inforich.disposal.service.IDisposalLandTerminalVehicleApproachService;

/**
 * 处置终端车辆进场明细Service业务层处理
 *
 * @author hjm
 * @date 2022-10-25
 */
@Service
public class DisposalLandTerminalVehicleApproachServiceImpl extends
    ServiceImpl<DisposalLandTerminalVehicleApproachMapper, DisposalLandTerminalVehicleApproach> implements
    IDisposalLandTerminalVehicleApproachService {

  @Override
  public Page<DisposalLandTerminalVehicleApproach> pageDisposalLandTerminalVehicleApproach(
      Page<DisposalLandTerminalVehicleApproach> page, Map<String, Object> map) {
    baseMapper.pageDisposalLandTerminalVehicleApproach(page, map);

    // 实重
    BigDecimal garbageWeight = page.getRecords().stream()
        .filter(l -> l.getGarbageWeight() != null)
        .map(DisposalLandTerminalVehicleApproach::getGarbageWeight)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal netWeight = page.getRecords().stream()
        .filter(l -> l.getNetWeight() != null)
        .map(DisposalLandTerminalVehicleApproach::getNetWeight)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal tare = page.getRecords().stream()
        .filter(l -> l.getTare() != null)
        .map(DisposalLandTerminalVehicleApproach::getTare)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal grossWeight = page.getRecords().stream()
        .filter(l -> l.getGrossWeight() != null)
        .map(DisposalLandTerminalVehicleApproach::getGrossWeight)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal deduction = page.getRecords().stream()
        .filter(l -> l.getDeduction() != null)
        .map(DisposalLandTerminalVehicleApproach::getDeduction)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal oneWeighing = page.getRecords().stream()
        .filter(l -> l.getOneWeighing() != null)
        .map(DisposalLandTerminalVehicleApproach::getOneWeighing)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal secondWeighing = page.getRecords().stream()
        .filter(l -> l.getSecondWeighing() != null)
        .map(DisposalLandTerminalVehicleApproach::getSecondWeighing)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal amount = page.getRecords().stream()
        .filter(l -> l.getAmount() != null)
        .map(DisposalLandTerminalVehicleApproach::getAmount)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    BigDecimal weighingFee = page.getRecords().stream()
        .filter(l -> l.getWeighingFee() != null)
        .map(DisposalLandTerminalVehicleApproach::getWeighingFee)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    DisposalLandTerminalVehicleApproach approach = new DisposalLandTerminalVehicleApproach();
    approach.setDisposalLandName("合计");
    approach.setGarbageWeight(garbageWeight);
    approach.setNetWeight(netWeight);
    approach.setTare(tare);
    approach.setGrossWeight(grossWeight);
    approach.setDeduction(deduction);
    approach.setOneWeighing(oneWeighing);
    approach.setSecondWeighing(secondWeighing);
    approach.setAmount(amount);
    approach.setWeighingFee(weighingFee);
    if (CollUtil.isNotEmpty(page.getRecords())) {
      page.getRecords().add(approach);
    }
    return page;
  }

  @Override
  public List<DisposalLandTerminalVehicleApproach> listDisposalLandTerminalVehicleApproach(
      Map<String, Object> map) {
    return baseMapper.listDisposalLandTerminalVehicleApproach(map);
  }


}
